<template>
    <div>
        <div class="table_box">
            <tyTable :searchFlag="true"
                     :listApi="$api.listServerInfo"
                     :deleteApi="$api.deleteUser"
                     :delete-flag="false"
                     :columnWidth="120"
                     :fixParamFunc="fixParamFunc"
                     :openDrawer="openDrawer" ref="tyTable">
                <template v-slot:search="{ param}">
                    <el-form-item label="conf文件:" prop="name">
                        <el-select v-model="param.confInfoId" placeholder="请选择">
                            <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="端口:" prop="name">
                        <el-input v-model="param.port" type="number" placeholder="请输入端口"></el-input>
                    </el-form-item>
                    <el-form-item label="是否启用:" prop="name">
                        <el-select v-model="param.isEnable" placeholder="请选择" clearable>
                            <el-option label="启用" :value="1"></el-option>
                            <el-option label="禁用" :value="0"></el-option>
                        </el-select>
                    </el-form-item>
                </template>

                <!--            按钮-->
                <template v-slot:search-operation>
                    <el-button type="primary" @click="$router.go(-1)" icon="el-icon-back">返回</el-button>
                </template>

                <template v-slot:table>
                    <el-table-column type="index" label="序号" align="center" width="52"></el-table-column>
                    <el-table-column prop="confInfoName" label="文件" align="center"></el-table-column>
                    <el-table-column prop="listen" label="端口" align="center"></el-table-column>
                    <el-table-column prop="serverName" label="服务名" align="center"></el-table-column>
                    <el-table-column prop="text" label="配置" align="center">
                        <template slot-scope="scope">
                            <div class="limit-height">{{scope.row.text}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="sort" label="是否启用" align="center">
                        <template slot-scope="scope">
                            <div v-if="scope.row.isEnable == 1" style="color: #67C23A">启用</div>
                            <div v-else style="color: #909399">禁用</div>
                        </template>
                    </el-table-column>
                </template>

                <template v-slot:extra="{row}">
                    <el-button @click="gotoLocationConfig(row)" type="text" size="small">
                        LOCATION
                    </el-button>
                </template>
            </tyTable>
        </div>

        <tyAddUpdate :model="formData"
                     :add-api="$api.addUser"
                     :update-api="$api.updateServerInfo"
                     :dialogSize="'60%'"
                     ref="tyAddUpdate"
        >
            <el-form label-position="left" label-width="100px" :model="formData" :rules="rules">
                <el-form-item label="文件:" prop="confInfoName">
                    <el-input v-model="formData.confInfoName" placeholder=""  clearable disabled
                              maxlength="300"></el-input>
                </el-form-item>
                <el-form-item label="端口:" prop="listen">
                    <el-input v-model="formData.listen" placeholder="" type="number"  clearable
                              maxlength="300"></el-input>
                </el-form-item>
                <el-form-item label="服务名:" prop="serverName">
                    <el-input v-model="formData.serverName" placeholder=""  clearable
                              maxlength="300"></el-input>
                </el-form-item>
                <el-form-item label="是否启用:" prop="isEnable">
                    <el-switch
                        v-model="formData.isEnable"
                        active-color="#13ce66"
                        :active-value="1"
                        :inactive-value="0"
                        >
                    </el-switch>

                </el-form-item>

                <el-form-item label="内容">
                    <el-input v-model="formData.text" type="textarea" autosize placeholder=""
                    ></el-input>
                </el-form-item>

            </el-form>
        </tyAddUpdate>

    </div>
</template>

<script>
import {tyAddUpdateMixins, tyConfirmMixins} from "@/mixins/addEditFormMixins";

export default {
    name: "serverConfig",
    mixins: [tyAddUpdateMixins, tyConfirmMixins],
    data(){
        return{
            formData : {},
            loading:false,
            options: [],

            rules: {
                confInfoName: [
                    {required: true, message: "文件不能为空", trigger: "blur"}
                ],
                listen: [
                    {required: true, message: "端口不能为空", trigger: "blur"}
                ],
                serverName: [
                    {required: true, message: "服务名不能为空", trigger: "blur"}
                ],

            }
        }
    },
    methods : {
        getSelect(){
            this.$api.listConfInfo({page : 1 , limit : 1000}).then((res) => {
                this.options = []
                let array = res.records
                for(let item of array){
                    this.options.push({value : item.id, label : item.path})
                }
            })
        },
        gotoLocationConfig(row){
            this.$router.push({name : 'locationConfig' , query:{listen : row.listen}})
        },
        fixParamFunc(form){
            let confInfoId = this.$route.query.confInfoId
            if(confInfoId !=  null){
                form.confInfoId = confInfoId
            }
        }
    },
    mounted() {
        this.getSelect()
    },

}

</script>
<style scoped lang="scss">
    .limit-height{
        height:50px;
    }
</style>